//
//  YYT_ChannelItem1ViewController.m
//  AudioVisualFeast
//
//  Created by dllo on 16/2/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "QL_ChannelItem1ViewController.h"
#import "QL_ChannelModle.h"
#import "QL_MVPlayerViewController.h"
#import "QL_ModleShared.h"
#import "QL_AFNTools.h"
#import "QL_item1TableViewCell.h"
#import "QL_MVPlayerViewController.h"
#import <MJRefresh.h>

@interface QL_ChannelItem1ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain)NSMutableArray *itemDataSource;
@property (nonatomic, retain)UITableView *tableview;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, assign) NSInteger bianliang;

@end

@implementation QL_ChannelItem1ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //    导航栏颜色
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"MVfanhui"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(left1:)];
    
    self.title = self.biaoti;
    
    self.itemDataSource = [NSMutableArray arrayWithCapacity:0];
    
    [self.view addSubview:self.tableview];
    
    self.bianliang = self.chuanzhi;
    [self getData:self.num with:self.bianliang];
    
    [self addheader];
    
    [self addfooter];
    
}

//http://mapi.yinyuetai.com/channel/videos.json?D-A=0&channelId=73&detail=true&offset=0&order=VideoPubDate&size=10
- (void)getData:(NSInteger)dataNum with:(NSInteger)bianliang{
    [QL_AFNTools getWithUrl:@"http://mapi.yinyuetai.com/channel/videos.json?D-A=0" parameter:@{@"channelId":[NSString stringWithFormat:@"%ld", bianliang],@"detail":@"true",@"offset":[NSString stringWithFormat:@"%ld", dataNum],@"order":@"VideoPubDate",@"size":@"10"} httpHeader:@{@"App-Id":@"10101026",@"Device-Id":@"587d897556663eb999e31a4c621abfbc",@"Device-V":@"aVBob25lIE9TXzkuMl82NDAqMTEzNl8xMDAwMDEwMDBfaVBob25lNiwy"} resposeType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dictionary = [[NSDictionary alloc] initWithDictionary:result];
        NSArray *array = dictionary[@"videos"];
        for (NSDictionary *dic in array) {
            QL_ModleShared *model = [[QL_ModleShared alloc] initWithDictionary:dic];
            [self.itemDataSource addObject:model];
        }
        [self.tableview.mj_header endRefreshing];
        [_tableview reloadData];
        
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
}

// 懒加载TableView
- (UITableView *)tableview {
    if (!_tableview) {
        self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height ) style:UITableViewStyleGrouped];
        [_tableview registerClass:[QL_item1TableViewCell class] forCellReuseIdentifier:@"YYT_item1CellIdentifier"];
        _tableview.backgroundColor = [UIColor blackColor];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = NO;
        [self.view addSubview:self.tableview];
        
    }
    return _tableview;
}

- (void)left1:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- tableview协议
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    QL_item1TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YYT_item1CellIdentifier"];
    if (self.itemDataSource.count > 0) {
        if (indexPath.row < self.itemDataSource.count) {
            cell.model = self.itemDataSource[indexPath.row];
        }
    }
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_itemDataSource.count > 0) {
        return _itemDataSource.count;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

#pragma mark -- 上拉刷新
-(void)addfooter {
    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        // 进入刷新状态后会自动调用这个block
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.num += 20;
            [self getData:self.num with:self.bianliang];
            [self.tableview.mj_footer endRefreshing];
        });
    }];
    
}
#pragma mark -- 下拉刷新
-(void)addheader {
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.num = 0;
            [self.itemDataSource removeAllObjects];
            [self getData:self.num with:self.bianliang];
            [self.tableview.mj_header endRefreshing];
        });
    }];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    QL_MVPlayerViewController *mvplay = [[QL_MVPlayerViewController alloc] init];
    mvplay.playurl = [self.itemDataSource[indexPath.row]urls]; // 传一个每个cell 相对应的网址、
    mvplay.headtitle = [self.itemDataSource[indexPath.row] title];// 传一个标题
    mvplay.ids = [NSString stringWithFormat:@"%@", [self.itemDataSource[indexPath.row] ids]];
    mvplay.pic = [NSString stringWithFormat:@"%@",[self.itemDataSource[indexPath.row] playListPic]];
    [self.navigationController pushViewController:mvplay animated:NO];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
